package com.code.qgjx.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

public class MyTool {
	public static String SaveImg(MultipartFile file, String dir, String imgName) {
		// 获取项目根目录
		String projectPath = System.getProperty("user.dir");
		// 在项目根目录下创建uploads文件夹
		String uploadPath = projectPath + "/src/main/resources/static/Sys/uploads/";
		File direction = new File(uploadPath);
		if (!direction.exists()) {
			direction.mkdirs();
		}
		
		File myFile = new File(direction, imgName);
		System.out.println("addr:" + myFile.getAbsolutePath());
		try {
			FileInputStream fis = (FileInputStream) file.getInputStream();
			FileOutputStream fos = new FileOutputStream(myFile);
			byte[] b = new byte[1024];
			int len;
			while ((len = fis.read(b)) != -1) {
				fos.write(b, 0, len);
			}
			fos.close();
			fis.close();
			return imgName;  // 只返回文件名，不返回完整路径
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getImg() {
		String projectPath = System.getProperty("user.dir");
		return projectPath + "/src/main/resources/static/uploads/";
	}

}
